package com.dtwave.lnstreaming.connection;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//自动导入get set以及构造方法
@Data
@AllArgsConstructor
public class HbaseConnector {
    private final static Logger LOGGER  = LoggerFactory.getLogger(HbaseConnector.class);

    private static Configuration conf = null;
    private static Connection connection = null;
    private static ExecutorService pool = null;

    /**
     * 获取全局唯一的Configuration实例
     */
    public static synchronized Configuration getConfiguration(String quorum,String address) {
        if(conf == null){
            conf = HBaseConfiguration.create();
            conf.set(quorum,address);
        }
        return conf;
    }

    /**
     * 获取全局唯一的connection实例
     * @return
     */
    public static synchronized Connection getConnection(String quorum,String address,Integer poolNum) throws Exception {
        if(connection == null){
            synchronized (HbaseConnector.class){
                if(connection == null){
                    LOGGER.info("获取全局唯一的HbaseClient实例");
                    pool =  Executors.newFixedThreadPool(poolNum);
                    connection = ConnectionFactory.createConnection(getConfiguration(quorum,address),pool);
                }
            }
        }
        return connection;
    }
}